package com.itheima.controller;

import com.itheima.common.Code;
import com.itheima.common.MessageConstant;
import com.itheima.common.Result;
import com.itheima.service.StatisticsService;
import com.itheima.vo.StaCateVo;
import com.itheima.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 后端首页 - 平台数据展示
 *
 * @author LIFE
 * @date 2022-06-05 07:28:32
 **/
@RestController
@Slf4j
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    /**
     * 功能描述: 获取菜品和套餐数量
     *
     * @return :com.itheima.common.Result
     */
    @GetMapping("/getCount")
    public Result getDish2MealCount() {
        List<Integer> dish2MealCount = statisticsService.getDish2MealCount();
        if (dish2MealCount != null) {
            return new Result(Code.OK, MessageConstant.DISPLAY_OK, dish2MealCount);
        }
        return new Result(Code.ERROR, MessageConstant.DISPLAY_ERROR);
    }

    @GetMapping("/getUserCount")
    public Result getUserCount() {
        List<UserVO> userCounts = statisticsService.getUserCounts();
        if (userCounts == null) {
            return new Result(Code.ERROR, MessageConstant.USER_COUNTS_ERROR);
        }
        Integer sum = 0;
        for (UserVO userCount : userCounts) {
            sum += userCount.getCounts();
            userCount.setCounts(sum);
        }
        return new Result(Code.OK, MessageConstant.USER_COUNTS_OK, userCounts);
    }

    @GetMapping("/getCateSta")
    public Result getCateCount() {
        List<StaCateVo> cateCount = statisticsService.getCateCount();
        if (cateCount == null) {
            return new Result(Code.ERROR, MessageConstant.USER_COUNTS_ERROR);
        }
        return new Result(Code.OK, MessageConstant.USER_COUNTS_OK, cateCount);
    }
}
